package com.dayaokeji.rhythmschoolstudent.client.common.vote;


import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.dayaokeji.rhythmschoolstudent.R;
import com.dayaokeji.rhythmschoolstudent.client.common.ListFragment;
import com.dayaokeji.rhythmschoolstudent.client.common.vote.adapter.VoteAdapter;
import com.dayaokeji.rhythmschoolstudent.event.UpdateVoteEvent;
import com.dayaokeji.rhythmschoolstudent.utils.DialogUtils;
import com.dayaokeji.rhythmschoolstudent.utils.ServerCallback;
import com.dayaokeji.rhythmschoolstudent.utils.ToastUtils;
import com.dayaokeji.server_api.ApiUtils;
import com.dayaokeji.server_api.ServerResponse;
import com.dayaokeji.server_api.api.VoteApi;
import com.dayaokeji.server_api.domain.UpdateVoteStatus;
import com.dayaokeji.server_api.domain.Vote;
import com.yqritc.recyclerviewflexibledivider.HorizontalDividerItemDecoration;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import retrofit2.Call;

/**
 * 投票列表
 */
public class VoteListFragment extends ListFragment<Vote,BaseViewHolder> {

    public static final String EXTRA_TEACHER_ID = "teacher_id";
    public static final String EXTRA_REL_ID = "rel_id";
    public static final String EXTRA_REL_TYPE = "rel_type";
    private int teacherId;
    private int relId;
    private int relType;

    private VoteAdapter voteAdapter;

    public VoteListFragment() {
    }

    public static VoteListFragment newInstance(int teacherId,int relId,int relType) {

        Bundle args = new Bundle();
        args.putInt(EXTRA_TEACHER_ID,teacherId);
        args.putInt(EXTRA_REL_ID,relId);
        args.putInt(EXTRA_REL_TYPE,relType);
        VoteListFragment fragment = new VoteListFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected Call createCall(int offset, int max) {
        return ApiUtils.getApi(VoteApi.class).list(teacherId,relId,relType,offset,max);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        Bundle arguments = getArguments();
        teacherId = arguments.getInt(EXTRA_TEACHER_ID);
        relId = arguments.getInt(EXTRA_REL_ID);
        relType = arguments.getInt(EXTRA_REL_TYPE);

        init();

        setOnListener();
    }

    private void init() {
        voteAdapter = new VoteAdapter();
        setAdapter(voteAdapter);
        getAdapter().setEmptyView(R.layout.empty_layout,getRecyclerView());
    }

    private void setOnListener() {
        getAdapter().setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter baseQuickAdapter, View view, int i) {
                int size = baseQuickAdapter.getData().size();
                if (i < size) {
                    Vote vote = (Vote) baseQuickAdapter.getData().get(i);
                    Intent intent = new Intent(getActivity(), VoteDetailActivity.class);
                    intent.putExtra(VoteDetailActivity.EXTRA_VOTE_ENTITY,vote);
                    startActivity(intent);
                }
            }
        });

    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void update(UpdateVoteEvent updateVoteEvent) {
        onRefresh();
    }

    @Override
    public void onDestroy() {
        EventBus.getDefault().unregister(this);
        super.onDestroy();
    }
}
